package com.ik.hiddevice.utlis;

import android.bluetooth.BluetoothDevice;
import android.content.ComponentName;
import android.content.pm.PackageManager;
import android.util.Log;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;

public class ClsUtils {
    /**
     * 与设备配对 参考源码：platform/packages/apps/Settings.git
     * /Settings/src/com/android/settings/bluetooth/CachedBluetoothDevice.java
     */
    static public boolean createBond(Class btClass, BluetoothDevice btDevice)

    {
        boolean returnValue;
        try {
            Method createBondMethod = btClass.getMethod("createBond");
            returnValue = (Boolean) createBondMethod.invoke(btDevice);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }

        return returnValue;
    }

    /**
     * 与设备解除配对 参考源码：platform/packages/apps/Settings.git
     * /Settings/src/com/android/settings/bluetooth/CachedBluetoothDevice.java
     */
    static public boolean removeBond(Class<?> btClass, BluetoothDevice btDevice)
    
    {
        boolean returnValue = false;
        try {
            Method removeBondMethod = btClass.getMethod("removeBond");
            returnValue = (boolean) removeBondMethod.invoke(btDevice);
        }catch (Exception e){e.printStackTrace();}
        
        return returnValue;
    }

    static public boolean isConnected(Class<?> btClass, BluetoothDevice btDevice)
            
    {
        boolean returnValue = false;
        try {
            Method removeBondMethod = btClass.getMethod("isConnected");
            returnValue = (Boolean) removeBondMethod.invoke(btDevice);
        }catch (Exception e){e.printStackTrace();}

        return returnValue;
    }

    static public boolean setPin(Class<? extends BluetoothDevice> btClass, BluetoothDevice btDevice,
                                 String str) throws Exception
    {
        try
        {
            Method removeBondMethod = btClass.getDeclaredMethod("setPin",
                    new Class[]
                            {byte[].class});
            Boolean returnValue = (Boolean) removeBondMethod.invoke(btDevice,
                    new Object[]
                            {str.getBytes()});
            Log.e("returnValue", "" + returnValue);
        }
        catch (SecurityException e)
        {
            // throw new RuntimeException(e.getMessage());
            e.printStackTrace();
        }
        catch (IllegalArgumentException e)
        {
            // throw new RuntimeException(e.getMessage());
            e.printStackTrace();
        }
        catch (Exception e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return true;

    }

    // 取消用户输入
    static public boolean cancelPairingUserInput(Class<?> btClass,
                                                 BluetoothDevice device)
    {
        boolean returnValue = false;
        try {
            Method createBondMethod = btClass.getMethod("cancelPairingUserInput");

            returnValue = (boolean) createBondMethod.invoke(device);
        }catch (Exception e){e.printStackTrace();}
        
        return returnValue;
    }

    // 取消配对
    static public boolean cancelBondProcess(Class<?> btClass,
                                            BluetoothDevice device)

    
    {
        boolean returnValue = false;
        try {
            Method createBondMethod = btClass.getMethod("cancelBondProcess");
            returnValue = (boolean) createBondMethod.invoke(device);
        }catch (Exception e){e.printStackTrace();}
        
        return returnValue;
    }

    //确认配对

    static public void setPairingConfirmation(Class<?> btClass, BluetoothDevice device, boolean isConfirm)throws Exception
    {
        Method setPairingConfirmation = btClass.getDeclaredMethod("setPairingConfirmation",boolean.class);
        setPairingConfirmation.invoke(device,isConfirm);
    }


    /**
     *
     * @param clsShow
     */
    static public void printAllInform(Class clsShow)
    {
        try
        {
            // 取得所有方法
            Method[] hideMethod = clsShow.getMethods();
            int i = 0;
            for (; i < hideMethod.length; i++)
            {
                Log.e("method name", hideMethod[i].getName() + ";and the i is:"
                        + i);
            }
            // 取得所有常量
            Field[] allFields = clsShow.getFields();
            for (i = 0; i < allFields.length; i++)
            {
                Log.e("Field name", allFields[i].getName());
            }
        }
        catch (SecurityException e)
        {
            // throw new RuntimeException(e.getMessage());
            e.printStackTrace();
        }
        catch (IllegalArgumentException e)
        {
            // throw new RuntimeException(e.getMessage());
            e.printStackTrace();
        }
        catch (Exception e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    static public boolean connect(Object obj,BluetoothDevice device)
    {
        boolean result;
        try {
//            Log.e("xxxxx","connect obj = "+obj);
            Method connect = obj.getClass().getMethod("connect",new Class[]{device.getClass()});
            result = (boolean) connect.invoke(obj,new Object[]{device});
        }catch (Exception e)
        {

            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        }
        return result;
    }

    static public boolean setPriority(Object obj, BluetoothDevice device,int priority)
    {
        boolean result;
        try {
            Method setPriority = obj.getClass().getMethod("setPriority", new Class[]{BluetoothDevice.class, int.class});
            result = (boolean) setPriority.invoke(obj, new Object[]{device, priority});
        }catch (Exception e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        }
        return result;
    }

    static public void getConnectedDevices(Object obj)
    {

        try {
            Log.e("getConnectedDevices","connect obj = "+obj);
            Method connect = obj.getClass().getDeclaredMethod("getConnectedDevices");
            List<BluetoothDevice> devices = (List<BluetoothDevice>) connect.invoke(obj);
            for (BluetoothDevice device1:devices){
                Log.e("getConnectedDevices","connect device = "+device1.getName());
            }
        }catch (Exception e)
        {

            // TODO Auto-generated catch block
            e.printStackTrace();

        }

    }
    static public ComponentName resolveSystemService(Object obj, PackageManager packageManager,int flags)
    {
        ComponentName comp;
        try {
            Method resolveSystemService = obj.getClass().getDeclaredMethod("resolveSystemService",PackageManager.class,int.class);
            comp = (ComponentName) resolveSystemService.invoke(obj,packageManager,flags);
            return comp;
        }catch (Exception e)
        {
            e.printStackTrace();
            return null;
        }

    }

    static public void getAllMethod(Object obj)
    {

        try {

            for(Method method:obj.getClass().getMethods())
            {
                Log.e("getMethods","obj = "+obj+" , method="+method.getName());
            }
            for(Method method:obj.getClass().getDeclaredMethods())
            {
                Log.e("getDeclaredMethods","obj = "+obj+" , method="+method.getName());
            }
        }catch (Exception e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();

        }

    }

}
